package com.sxbbc.api.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.sxbbc.common.core.entity.jz.*;
import com.sxbbc.common.core.enums.HandleTypeEnums;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.function.common.GetRest;
import com.function.common.RestResponse;
import com.sxbbc.api.common.AccountMapping;
import com.sxbbc.api.service.ICustomerService;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.basics.utils.token.TokenUtils;
import com.sxbbc.common.core.dto.jz.TransVO;
import com.sxbbc.common.core.enums.CashTypeEnums;
import com.sxbbc.common.core.service.IAccountChangeService;
import com.sxbbc.common.core.service.IBankDetailService;
import com.sxbbc.common.core.service.IBankTypeService;
import com.sxbbc.common.core.service.ICDonateDetailService;
import com.sxbbc.common.core.service.ICFreezePointDetailService;
import com.sxbbc.common.core.service.ICMoneyDetailService;
import com.sxbbc.common.core.service.ICPointDetailService;
import com.sxbbc.common.core.service.ICShareDetailService;
import com.sxbbc.common.core.service.IInfoParamService;
import com.sxbbc.common.core.service.ISysCashDetailsService;
import com.sxbbc.common.core.service.ISysCashRuleService;
import com.sxbbc.common.core.service.ITransService;
import com.sxbbc.common.core.service.system.login.LoginTokenService;
import com.sxbbc.common.core.utils.BasePage;

/**
 * 用户账户信息 控制层
 * @author xiaoky
 * @date 2019-05-31
 */
@RestController
public class AccountController {
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private LoginTokenService loginTokenService;
	@Autowired
	private ICMoneyDetailService moneyDetailService;
	@Autowired
	private ICPointDetailService pointDetailService;
	@Autowired
	private ISysCashRuleService sysCashRuleService;
	@Autowired
	private IBankDetailService bankDetailService;
	@Autowired
	private IBankTypeService bankTypeService;
	@Autowired
	private ICustomerService customerService;
	@Autowired
	private ICDonateDetailService donateDetailService;
	@Autowired
	private ICShareDetailService shareDetailService;
	@Autowired
	private IInfoParamService infoParamService;
	@Autowired
	private ICFreezePointDetailService cFreezePointDetailService;
	@Autowired
	private ISysCashDetailsService sysCashDetailsService;
	@Autowired
	private IAccountChangeService accountChangeService;
	@Autowired
	private ITransService iTransService;
	
	/**
	 * 查询用户账户信息
	 * @param token 获取cid
	 * @return
	 */
	@RequestMapping(value = AccountMapping.QUERY_CUSTOMER_ACCOUNT)
	public RestResponse queryCustomerAccount (String token) {
		Integer cid = loginTokenService.validLoginCid(token);
		if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
		Map<String, Object> map = customerService.queryCustomerAccount(cid);
		return GetRest.getSuccess("", map);
	}
	
	/**
	 * 查询用户余额明细
	 * @param token 获取cid
	 * @return 余额明细分页数据
	 */
	@RequestMapping(value = AccountMapping.QUERY_MONEY_DETAIL)
	public RestResponse queryMoneyDetail (BasePage<CMoneyDetail> page, String token) {
		Integer cid = loginTokenService.validLoginCid(token);
		if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
		// 查询条件赋值
		CMoneyDetail moneyDetail = new CMoneyDetail();
		moneyDetail.setCid(cid);
		IPage<CMoneyDetail> pageInfo = moneyDetailService.queryCustomerMoneyList(page, moneyDetail);
		return GetRest.getSuccess("", pageInfo);
	}


	/**
	 * 查询冻结用户余额明细
	 * @param token 获取cid
	 * @return 余额明细分页数据
	 */
	@RequestMapping(value = AccountMapping.QUERY_FREEZE_MONEY_DETAIL)
	public RestResponse queryFreezeMoneyDetail (BasePage<CFreezeMoneyDetail> page, String token) {
		Integer cid = loginTokenService.validLoginCid(token);
		if (StringUtils.isBlank(token) || cid == null) {
			return GetRest.getFail("获取用户信息失败");
		}
		// 查询条件赋值
		CFreezeMoneyDetail moneyDetail = new CFreezeMoneyDetail();
		moneyDetail.setCid(cid);
		IPage<CFreezeMoneyDetail> pageInfo = moneyDetailService.queryCustomerFreezeMoneyList(page, moneyDetail);
		return GetRest.getSuccess("", pageInfo);
	}
	
	/**
	 * 查询用户积分明细
	 * @param token 获取cid
	 * @return 积分明细分页数据
	 */
	@RequestMapping(value = AccountMapping.QUERY_POINT_DETAIL)
	public RestResponse queryPointDetail (BasePage<CPointDetail> page, String token) {
		Integer cid = loginTokenService.validLoginCid(token);
		if (StringUtils.isBlank(token) || cid == null) {
			return GetRest.getFail("获取用户信息失败");
		}
		// 查询条件赋值
		CPointDetail pointDetail = new CPointDetail();
		pointDetail.setCid(cid);
		IPage<CPointDetail> pageInfo = pointDetailService.queryCustomerPointList(page, pointDetail);
		return GetRest.getSuccess("", pageInfo);
	}

	/**
	 * 查询用户基金明细
	 * @param token 获取cid
	 * @return 积分明细分页数据
	 */
	@RequestMapping(value = AccountMapping.QUERY_FUND_DETAIL)
	public RestResponse queryFundDetail (BasePage<CMoneyDetail> page, String token) {
		Integer cid = loginTokenService.validLoginCid(token);
		if (StringUtils.isBlank(token) || cid == null) {
			return GetRest.getFail("获取用户信息失败");
		}
		// 查询条件赋值
		CMoneyDetail moneyDetail = new CMoneyDetail();
		moneyDetail.setCid(cid);
		moneyDetail.setTypeId(HandleTypeEnums.FUND.getId());
		IPage<CMoneyDetail> pageInfo = moneyDetailService.queryCustomerMoneyList(page, moneyDetail);
		return GetRest.getSuccess("", pageInfo);
	}

	/**
	 * 获取用户提现规则
	 * @return 
	 */
	@RequestMapping(value = AccountMapping.GET_CUS_CASH_RULE)
	public RestResponse getCusCashRule () {
		Map<String, Object> map = sysCashRuleService.getCashRule(CashTypeEnums.CUSTOMER.getId());
		return GetRest.getSuccess("", map);
	}
	
	/**
	 * 查询用户银行卡列表
	 * @param token 获取cid
	 * @return 结果集
	 */
	@RequestMapping(value = AccountMapping.QUERY_BANK_DETAIL_LIST)
	public RestResponse queryBankDetailList (String token) {
		Integer cid = loginTokenService.validLoginCid(token);
		if (StringUtils.isBlank(token) || cid == null) {
			return GetRest.getFail("获取用户信息失败");
		}
		List<BankDetail> bankDetailList = bankDetailService.queryBankDetailList(cid, CashTypeEnums.CUSTOMER.getId());
		return GetRest.getSuccess("", bankDetailList);
	}
	
	/**
	 * 删除银行卡
	 * @param token 获取cid
	 * @param bankDetailId 银行卡id
	 * @return 处理结果
	 */
	@RequestMapping(value = AccountMapping.DELETE_BANK_DETAIL)
	public RestResponse detailBankDetail (String token, Integer bankDetailId) {
		Integer cid = loginTokenService.validLoginCid(token);
		if (StringUtils.isBlank(token) || cid == null) {
			return GetRest.getFail("获取用户信息失败");
		}
		RestResponse result = bankDetailService.detailBankDetail(cid, bankDetailId, CashTypeEnums.CUSTOMER.getId());
		return result;
	}
	
	/**
	 * 查询银行卡类型列表
	 * @return 结果集
	 */
	@RequestMapping(value = AccountMapping.QUERY_BANK_TYPE_LIST)
	public RestResponse queryBankTypeList () {
		QueryWrapper<BankType> wrapper = new QueryWrapper<BankType>();
		wrapper.eq("status", StaticUtils.STATUS_YES);
		List<BankType> list = bankTypeService.list(wrapper);
		return GetRest.getSuccess("", list);
	}
	
	/**
	 * 添加银行卡
	 * @param token 获取cid
	 * @param detail 银行卡对象
	 * @return 处理结果
	 */
	@RequestMapping(value = AccountMapping.ADD_BANK_DETAIL)
	public RestResponse addBankDetail (String token, BankDetail detail) {
		Integer cid = loginTokenService.validLoginCid(token);
		if (StringUtils.isBlank(token) || cid == null) {
			return GetRest.getFail("获取用户信息失败");
		}
		detail.setTargetId(cid);
		detail.setType(CashTypeEnums.CUSTOMER.getId());
		RestResponse result = bankDetailService.editBankDetail(detail);
		return result;
	}
	
	/**
	 * 处理用户提现
	 * @param cashDetail 参数
	 * @return 申请提现结果
	 */
	@RequestMapping(value = AccountMapping.HANDLE_CUSTOMER_CASH)
	public RestResponse handleCustomerCash (String token, String payWord, SysCashDetails cashDetail) {
		Integer cid = loginTokenService.validLoginCid(token);
		if (StringUtils.isBlank(token) || cid == null) {
			return GetRest.getFail("获取用户信息失败");
		}
		cashDetail.setTargetId(cid);
		cashDetail.setType(CashTypeEnums.CUSTOMER.getId());
		return customerService.handleCustomerCashApply(cashDetail, payWord);
	}
	
	/**
	 * 处理用户充值
	 * @param token 获取cid
	 * @param cPayLog 充值信息
	 * @return
	 * @author xiaoky
	 */
	@RequestMapping(value = AccountMapping.HANDLE_CUSTOMER_RECHARGE)
	public RestResponse handleCustomerRecharge (String token, String payWord, CPayLog cPayLog) {
		Integer cid = loginTokenService.validLoginCid(token);
		if (StringUtils.isBlank(token) || cid == null) {
			return GetRest.getFail("获取用户信息失败");
		}
		cPayLog.setCid(cid);
		return customerService.handleCustomerRecharge(payWord, cPayLog);
	}
	
	/**
	 * 查询冻结积分详情
	 * @param page 分页
	 * @return 返回分页数据
	 * @author xiaoky
	 */
	@RequestMapping(value = AccountMapping.QUERY_FREEZE_POINT_DETAIL)
	public RestResponse queryFreezePointDetail (BasePage<CFreezePointDetail> page) {
		String token = TokenUtils.getToken(request);
		Integer cid = loginTokenService.validLoginCid(token);
		if (StringUtils.isBlank(token) || cid == null) {
			return GetRest.getFail("获取用户信息失败");
		}
		// 查询条件赋值
		CFreezePointDetail freezePointDetail = new CFreezePointDetail();
		freezePointDetail.setCid(cid);
		IPage<CFreezePointDetail> pageInfo = cFreezePointDetailService.queryCustomerFreezePointList(page, freezePointDetail);
		return GetRest.getSuccess("", pageInfo);
	}
	
	/**
	 * 通过交易编号查询提现记录
	 * @param payCode 交易编号
	 * @return 提现记录
	 * @author xiaoky
	 */
	@RequestMapping(value = AccountMapping.QUERY_CASH_BY_PAYCODE)
	public RestResponse queryCashByPaycode(String payCode) {
		String token = TokenUtils.getToken(request);
		Integer cid = loginTokenService.validLoginCid(token);
		if (StringUtils.isBlank(token) || cid == null) {
			return GetRest.getFail("获取用户信息失败");
		}
		// 查询提现记录
		QueryWrapper<SysCashDetails> queryWrapper = new QueryWrapper<SysCashDetails>();
		queryWrapper.eq("pay_code", payCode);
		SysCashDetails sysCash = sysCashDetailsService.getOne(queryWrapper);
		if (sysCash == null) {
			return GetRest.getFail("查询失败");
		}
		String code = sysCash.getBankCode();
		if(code.length()>4) {
			sysCash.setLastCode(code.substring(code.length()-4, code.length()));
		}else {
			sysCash.setLastCode(code);
		}
		String statusName = "";
		switch (sysCash.getStatus()) {
		case StaticUtils.STATUS_APPLY:
			statusName = "审核中";
			break;
		case StaticUtils.STATUS_SUCCESS:
			statusName = "提现成功";
			break;
		case StaticUtils.STATUS_FAIL:
			statusName = "被驳回";
			break;
		default:
			break;
		}
		sysCash.setStatusName(statusName);
		sysCash.setTargetId(null);
		sysCash.setBankId(null);
		return GetRest.getSuccess("", sysCash);
	}
	
	/**
	 * 转账
	 * @param vo 转账信息 
	 * @return
	 * @author Wahyee
	 */
	@RequestMapping(value=AccountMapping.HANDLE_TRANS,method=RequestMethod.POST)
	public RestResponse trans(TransVO vo) {
		Integer cid = loginTokenService.validLoginCid(TokenUtils.getToken(request));
		if (cid == null) {
			return GetRest.getFail("获取用户信息失败，请检查网络");
		}
		if(StringUtils.isBlank(vo.getKey())) {
			return  GetRest.getFail("请输入支付密码");
		}
		String r = iTransService.handleTrans(vo, cid);
		if(r.length() > 0) {
			return GetRest.getFail(r);
		}
		return  GetRest.getSuccess("转账成功");
	}
	
}
